package com.tuxing.draa_data.service.draa;

import com.google.common.base.Strings;
import com.tuxing.draa_data.dao.center.NewDcECollectionDao;
import com.tuxing.draa_data.dto.DraaDatabaseDto;
import com.tuxing.draa_data.pojo.center.NewDcECollection;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * @author yuanshihang
 * @version 1.0
 * @className DraaDatabaseServiceImpl
 * @description 基础描述:
 * @date 2021/12/2 20:53
 */
@Slf4j
@Service
public class DraaDatabaseServiceImpl implements DraaDatabaseService {

    @Resource
    private NewDcECollectionDao newDcECollectionDao;

    /**
     * @param databaseDto 数据库信息
     * @return 无返回结果
     * @decpription 进行draa数据库数据的解析和保存
     * @author yuanshihang
     * @date 2021/12/2 20:55
     * @version 1.0
     */
    @Override
    public void saveDraaDatabaseData(DraaDatabaseDto databaseDto) {

        if (databaseDto == null) {
            return;
        }

        NewDcECollection newDcECollection = new NewDcECollection();

        newDcECollection.setSourceId(databaseDto.getDbErId());
        newDcECollection.setCollectionName(databaseDto.getDbName());

        //数据库语种
        String language = databaseDto.getLanguage();
        if (!Strings.isNullOrEmpty(language)) {
            language = language.replace("文种 ： ", "").replace(" \n<br>", "");
            newDcECollection.setLang(language);
        }

        //所属学科
        String subject = databaseDto.getSubject();
        if (!Strings.isNullOrEmpty(subject)) {
            subject = subject.replace("所属学科 ： ", "").replace(" \n<br>", "");
            newDcECollection.setSubject(subject);
        }

        String type = databaseDto.getType();
        if (!Strings.isNullOrEmpty(type)) {
            String[] split = type.split("<br>");
            //文献揭示层次
            String level = split[0].replace("文献揭示层次 ： ", "").replace("\n", "").replace(" ", "");
            newDcECollection.setShowLevel(level);
            if (split.length > 1 && !Strings.isNullOrEmpty(split[1])) {
                String dataType = split[1].replace("收录文献类型 ：", "").replace(" \n<br>", "").replace("\n", "").replace(" ", "");
                newDcECollection.setDbType(dataType);
            }
        }

        //访问控制
        String visitControl = databaseDto.getVisitControl();
        if (!Strings.isNullOrEmpty(visitControl)) {
            String control = visitControl.replace("访问控制 ： ", "").replace(" \n<br>", "");
            newDcECollection.setAccessController(control);
        }

        //访问方式
        String visitMode = databaseDto.getVisitMode();
        if (!Strings.isNullOrEmpty(visitControl)) {
            String mode = visitMode.replace("访问方式 ： ", "").replace(" \n<br>", "");
            newDcECollection.setAccessType(mode);
        }

        //保存
        newDcECollectionDao.insert(newDcECollection);
    }
}
